package com.example.mine_platform.config;


import com.mysql.cj.log.Log;
import io.jsonwebtoken.Claims;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Slf4j
@WebFilter("/admin/*")
public class MyFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        log.info("对request进行过滤");
        String token = ((HttpServletRequest)servletRequest).getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String level=claims.get("level").toString();
        if(!level.equals("Admin")){
            servletResponse.setContentType("text/html;charset=utf-8");   //设置响应的字符集（避免中文乱码）
            servletResponse.getWriter().write("您无管理员权限");
        }
        else  filterChain.doFilter(servletRequest, servletResponse);
    }
}
